package com.itheima.module.web.controller.advice;

import com.itheima.module.web.vo.Result;
import com.itheima.module.web.vo.ResultCodeEnum;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@ControllerAdvice
public class MyExceptionHandler {
    /*@ExceptionHandler(Throwable.class)
    public ModelAndView catchException(Exception e){
        //这里 你想做啥  你随便
        System.out.println("我是错误处理逻辑");

        return new ModelAndView("error");
    }*/

    @ExceptionHandler(Throwable.class)
    @ResponseBody
    public Result catchException1(Exception e){
        //这里 你想做啥  你随便
        System.out.println("我是错误处理逻辑");

        return Result.failOf(ResultCodeEnum.SYSTEM_ERROR_B0001,null);
    }
    @ExceptionHandler(MissingServletRequestParameterException.class)
    @ResponseBody
    public Result catchException2(MissingServletRequestParameterException e){
        //这里 你想做啥  你随便
        System.out.println("异常是用户出问题");

        return Result.failOf(ResultCodeEnum.USER_ERROR_A0400,e.getParameterName());
    }
}
